<template>
  <div class="echarts-bar" ref="echartsBar"></div>
</template>

<script>
import * as echarts from "echarts";
export default {
  name: "EbarScroll",
  data() {
    return {
      chartInstance: null,
    };
  },
  props: {
    data: {
      type: Object,
      default: () => {
        return {
          xData: [],
          sData: [],
        };
      },
    },
  },
  mounted() {
    this.initChart();
    window.addEventListener("resize", this.screenAdapter);
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    initChart() {
      this.chartInstance = echarts.init(this.$refs.echartsBar);
      this.setOption();
    },
    setOption() {
      let titleFontSzie = (this.$refs.echartsBar.offsetWidth / 100) * 1.5;
      const initOption = {
        tooltip: {
          show: false,
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "rgba(0, 0, 0, 1)",
            },
          },
        },
        grid: {
          // top: "40%",
          // left: "5%",
          // right: "5%",
          bottom: "5%",
          // 把x轴和y轴纳入 grid
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: this.data.xData,
          axisLabel: {
            interval: 0,
            // rotate: 20,
            // fontSize: titleFontSzie,
            // fontWeight: "bold",
            // margin: 30,

            formatter: function (value) {
              let nameArr = value.split("：");
              if (nameArr.length > 1) {
                nameArr[1] = `${nameArr[1]}`;
                return (
                  "{smallSize|" +
                  nameArr[0] +
                  "}\n{largeSize|" +
                  nameArr[1] +
                  "}"
                );
              } else {
                return "{largeSize|" + nameArr[0] + "}";
              }
              // 使用rich属性来设置不同的字体大小
            },
            rich: {
              smallSize: {
                fontSize: titleFontSzie - 7, // 小的文字大小
                lineHeight: titleFontSzie + 7,
              },
              largeSize: {
                fontSize: titleFontSzie + 2, // 大的文字大小
                fontWeight: "bold",
              },
            },
          },
          axisTick: {
            // 刻度线
            show: false,
            lineStyle: {
              width: 2,
            },
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(255, 129, 1, 1)",
              width: 2,
            },
          },
        },
        yAxis: {
          show: false,
          type: "value",
          splitLine: {
            lineStyle: {
              color: "rgba(255, 129, 1, 1)",
              width: 2,
            },
          },
          axisLabel: {
            fontSize: 18,
            fontWeight: "bold",
          },
          axisTick: {
            show: false,
            lineStyle: {
              width: 2,
            },
          },
          axisLine: {
            show: false,
            lineStyle: {
              color: "rgba(255, 129, 1, 1)",
              width: 10,
            },
          },
        },
        series: [
          {
            data: this.data.sData,
            type: "bar",
            showBackground: true,
            label: {
              show: true,
              position: "top",
              color: "rgba(255, 129, 1, 1)",
              fontSize: titleFontSzie,
              fontWeight: "bold",
              // rotate: 30,
            },
            backgroundStyle: {
              // color: "rgba(255, 0, 0, 0.1)",
              color: "transparent",
            },
            itemStyle: {
              barBorderRadius: [10000, 10000, 0, 0],
              color: "rgba(255, 129, 1, 1)",
              // borderColor: "rgba(255, 0, 0, 0.65)",
              // borderWidth: 5,
              shadowColor: "rgba(255, 0, 0, 1)",
              shadowBlur: 30,
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.resize();
    },
    screenAdapter() {
      this.chartInstance.resize();
    },
  },
  watch: {
    data: {
      handler() {
        this.setOption();
      },
      deep: true,
    },
  },
};
</script>

<style lang="scss" scoped>
.echarts-bar {
  width: 100%;
  height: 100%;
}
</style>
